package com.yunfeisoft.controller.business;

import com.applet.base.BaseController;
import com.applet.utils.FastJsonUtils;
import com.applet.utils.Response;
import com.applet.utils.ResponseUtils;
import com.applet.utils.Validator;
import com.yunfeisoft.business.model.PmsSkuStock;
import com.yunfeisoft.business.service.inter.PmsSkuStockService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * ClassName: PmsSkuStockController
 * Description: 商品SKU表(商品库存量单位表)Controller
 * Author: Jackie liu
 * Date: 2020-06-02
 */
@Controller
public class PmsSkuStockController extends BaseController {

    @Autowired
    private PmsSkuStockService pmsSkuStockService;

    /**
     * 添加商品SKU表(商品库存量单位表)
     *
     * @param record
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsSkuStock/save", method = RequestMethod.POST)
    @ResponseBody
    public Response save(PmsSkuStock record, HttpServletRequest request, HttpServletResponse response) {
        pmsSkuStockService.save(record);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 修改商品SKU表(商品库存量单位表)
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsSkuStock/modify", method = RequestMethod.POST)
    @ResponseBody
    public Response modify(HttpServletRequest request, HttpServletResponse response) {
        Validator validator = new Validator();
        validator.required(request, "productId", "参数错误");
        validator.required(request, "skuStockListStr", "sku信息为空");
        if (validator.isError()) {
            return ResponseUtils.warn(validator.getMessage());
        }

        String productId = ServletRequestUtils.getStringParameter(request, "productId", null);
        String skuStockListStr = ServletRequestUtils.getStringParameter(request, "skuStockListStr", null);

        List<PmsSkuStock> stockList = FastJsonUtils.jsonToList(skuStockListStr, PmsSkuStock.class);
        for (PmsSkuStock skuStock : stockList) {
            if (skuStock.getPrice() == null) {
                return ResponseUtils.warn("商品SKU信息销售价格不全");
            }
            skuStock.setId(null);
            skuStock.setProductId(productId);
        }

        pmsSkuStockService.modify(stockList, productId);
        return ResponseUtils.success("保存成功");
    }

    /**
     * 查询商品SKU表(商品库存量单位表)
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsSkuStock/query", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response query(HttpServletRequest request, HttpServletResponse response) {
        String id = ServletRequestUtils.getStringParameter(request, "id", null);
        if (StringUtils.isBlank(id)) {
            return ResponseUtils.warn("参数错误");
        }
        PmsSkuStock record = pmsSkuStockService.load(id);
        return ResponseUtils.success(record);
    }

    /**
     * 分页查询商品SKU表(商品库存量单位表)
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsSkuStock/list", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response list(HttpServletRequest request, HttpServletResponse response) {
        String productId = ServletRequestUtils.getStringParameter(request, "productId", "_no_");
        List<PmsSkuStock> list = pmsSkuStockService.queryByProductId(productId);
        return ResponseUtils.success(list);
    }

    /**
     * 批量删除商品SKU表(商品库存量单位表)
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/web/pmsSkuStock/delete", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response) {
        String ids = ServletRequestUtils.getStringParameter(request, "ids", null);
        if (StringUtils.isBlank(ids)) {
            return ResponseUtils.warn("参数错误");
        }
        pmsSkuStockService.remove(ids.split(","));
        return ResponseUtils.success("删除成功");
    }
}
